package com.example.mylearn.tree;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;


/**
 * 二叉树的前中后遍历
 */
public class Tree {




    @Data
    public class Node<E>{
        E item;
        Node<E> left;
        Node<E> right;

        public Node(E item) {
            this.item = item;
        }

        public Node(E item, Node<E> left, Node<E> right) {
            this.item = item;
            this.left = left;
            this.right = right;
        }
    }


    public class TreeSort<E>{



        public <E> void preOrder(Node<E> root) {
            if (null != root) {
                System.out.print(root.item);
                preOrder(root.left);
                preOrder(root.right);
            }
        }

        public <E> void inOrder(Node<E> root) {
            if (null != root) {
                inOrder(root.left);
                System.out.print(root.item);
                inOrder(root.right);
            }
        }

        public <E> void postOrder(Node<E> root) {
            if (null != root) {
                postOrder(root.left);
                postOrder(root.right);
                System.out.print(root.item);
            }
        }





    }





    @Test
    public void test() {
        Node<String> root = new Node<>("A");
        root.left = new Node<>("B");
        root.right = new Node<>("C");

        root.left.left = new Node<>("D");
        root.left.right = new Node<>("E");
        root.left.left.left = new Node<>("H");

        root.right.left = new Node<>("F");
        root.right.right = new Node<>("G");


        TreeSort<String> treeSort = new TreeSort<>();
        treeSort.preOrder(root);
        System.out.println();
        treeSort.inOrder(root);
        System.out.println();
        treeSort.postOrder(root);





    }







}
